/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Source;

import java.sql.ResultSet;
import java.util.LinkedList;
/**
 *
 * @author JRN
 */
public class Rating_DAO {

     private String sentenciaSQL;
    ConexionDB conexion;
    LinkedList<Rating_POJO> listaDetalle = new LinkedList<Rating_POJO>();
    ResultSet resultado;

    public Rating_DAO(ConexionDB pConexion) {

        conexion = pConexion;
    }

     //Estructura Rating
                    //idRating int PRIMARY KEY,
                    //estrellas int,
                    //idUsuario int REFERENCES Usuario (idUsuario),
                    //rating int REFERENCES Usuario (idUsuario)

    public LinkedList<Rating_POJO> obtenerReg(int pIdRating) {

        sentenciaSQL = "SELECT * FROM Rating WHERE idRating=" + pIdRating;

        try {
            resultado = conexion.getResultSet(sentenciaSQL);

            if (resultado.next()) {
                Rating_POJO objRating = new Rating_POJO();

                objRating.setRating(Integer.parseInt(resultado.getString("idRating")));
                objRating.setEstrellas(Integer.parseInt(resultado.getString("estrellas")));
                objRating.setIdUsuario(Integer.parseInt(resultado.getString("idUsuario")));
                objRating.setRating(Integer.parseInt(resultado.getString("rating")));


                listaDetalle.add(objRating);
             

            }

        } catch (Exception ex) {
            System.out.println("error " + ex);
            return null;
        }

        return listaDetalle;
    }



    public boolean guardarReg(int idRating, int estrellas, int idUsuario, int rating) {

        
        sentenciaSQL = "INSERT INTO Rating(idRating,estrellas,idUsuario,rating) VALUES ("
                + idRating + "," + estrellas + "," + idUsuario + "," + rating + ")";

        try {
            conexion.executeQuery(sentenciaSQL);
            return true;
        

        } catch (Exception ex) {
            System.out.println("error " + ex);
            return false;
        }

    }



    public boolean actualizarReg(int idRating, int estrellas, int idUsuario, int rating) {

        sentenciaSQL = "UPDATE Rating SET estrellas = " + estrellas +
                        ",idUsuario = "+ idUsuario +
                        ",rating =" + rating +
                       " WHERE idRating = " + idRating;


        try {
            conexion.executeQuery(sentenciaSQL);
          
            return true;

        } catch (Exception ex) {
            System.out.println("error " + ex);
            return false;
        }
    }


     public boolean eliminarReg (int idRating){

        sentenciaSQL = "DELETE FROM Rating WHERE idRating="+idRating;

        try {
            conexion.executeQuery(sentenciaSQL);
            return true;

        } catch (Exception ex) {
            System.out.println("error " + ex);
            return false;
        }

    }

}
